/**   
 * @Title: ChangePasswordAct.java 
 * @Package： com.esun.rabbit2.viewer.mainact.mylotteryact.changepassword 
 * @Description: 修改密码
 * @Author Miko baoz@500wan.com   
 * @Date 2011-7-4 上午09:48:49  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.mylotteryact.changepassword;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.config.Language;
import com.esun.model.UserInfo;
import com.esun.network.ConnectionChangeReceiver;
import com.esun.rabbit2.communicaters.changepassword.ChangePasswordCommunicater;
import com.esun.rabbit2.viewer.LoginAct;
import com.esun.ui.R;
import com.esun.util.EditTextInputUitl;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;

/**
 * @ClassName: ChangePasswordAct
 * @Description: 修改密码Activity
 * @author Miko baoz@500wan.com
 * @date 2011-7-4 上午09:48:49
 * 
 */
public class ChangePasswordAct extends Activity implements OnClickListener {

	private SQLiteDatabase db;

	private EditText etOld;
	private EditText etNew;
	private EditText etVerify;

	private Button btnSubmit;
	private ImageButton btnGoBack;

	private Button btnClearOld;
	private Button btnClearNew;
	private Button btnClearVerify;

	private Handler handler;

	private static final int CHANGE_PASSWORD_SUCCESS = 100001;
	private static final int CHANGE_PASSWORD_FAILED = 100002;

	private String strOld;
	private String strNew;
	private String strVerify;

	private ProgressDialog progress;

	private boolean isChangePasswordSuccess = false;

	private ChangePasswordCommunicater communicater = new ChangePasswordCommunicater();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.change_password_act);

		init();
		initHandler();

	}

	private void init() {

		btnGoBack = (ImageButton) this.findViewById(R.id.change_password_btn_goBack);
		btnGoBack.setOnClickListener(this);

		btnSubmit = (Button) this.findViewById(R.id.change_password_act_btn_submit);
		btnSubmit.setOnClickListener(this);

		btnClearOld = (Button) this.findViewById(R.id.change_password_act_btn_claenOld);
		btnClearNew = (Button) this.findViewById(R.id.change_password_act_btn_claenNew);
		btnClearVerify = (Button) this.findViewById(R.id.change_password_act_btn_claenVerify);

		etOld = (EditText) this.findViewById(R.id.change_password_act_et_oldPwd);
		etOld = new EditTextInputUitl(etOld, btnClearOld).getEditText();

		etNew = (EditText) this.findViewById(R.id.change_password_act_et_newPwd);
		etNew = new EditTextInputUitl(etNew, btnClearNew).getEditText();

		etVerify = (EditText) this.findViewById(R.id.change_password_act_et_verifyPwd);
		etVerify = new EditTextInputUitl(etVerify, btnClearVerify).getEditText();

		progress = ViewerUtil.showProgressDialog(this, "请稍后");
	}

	private void initHandler() {

		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				progress.dismiss();

				switch (msg.what) {

				case CHANGE_PASSWORD_SUCCESS:

					clearUserInfo();
					clearDataBase();
					isChangePasswordSuccess = true;
					ViewerUtil.showTipDialog(ChangePasswordAct.this, "修改密码成功！请重新登陆！", true);

					break;

				case CHANGE_PASSWORD_FAILED:

					isChangePasswordSuccess = false;
					ViewerUtil.showTipDialog(ChangePasswordAct.this, communicater.getErrorMSG(), false);

					break;

				case Config.CONNECTION_FAIL:

					//ViewerUtil.showTipDialog(ChangePasswordAct.this, Language.STR_CONNECTIONFAIL, false);
					Toast.makeText(ChangePasswordAct.this, Language.STR_CONNECTIONFAIL.toString(), 0).show();
					break;

				default:
					break;

				}

			}
		};
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {

		case R.id.change_password_btn_goBack:

			this.finish();

			break;

		case R.id.change_password_act_btn_submit:

			if (checkInput()) {
				progress.show();
				startCommit();
			}

			break;

		case R.id.change_password_act_btn_claenOld:

			etOld.setText("");
			v.setVisibility(View.GONE);

			break;
		}
	}

	private boolean checkInput() {

		strOld = etOld.getText().toString();
		strNew = etNew.getText().toString();
		strVerify = etVerify.getText().toString();

		if (strOld.length() < 6 || strOld.length() > 15) {
			ViewerUtil.showTipDialog(this, "密码长度需为6-15位，请重新输入！", false);
			return false;
		} else if (strNew.length() < 6 || strNew.length() > 15) {
			ViewerUtil.showTipDialog(this, "密码长度需为6-15位，请重新输入！", false);
			return false;
		} else if (strVerify.length() < 6 || strVerify.length() > 15) {
			ViewerUtil.showTipDialog(this, "密码长度需为6-15位，请重新输入！", false);
			return false;
		} else if (!strNew.equals(strVerify)) {
			ViewerUtil.showTipDialog(this, "两次输入的密码不一致", false);
			return false;
		} else {
			return true;
		}
	}

	private void startCommit() {
		new Thread() {

			@Override
			public void run() {
				super.run();

				doCommit();

			}
		}.start();

	}

	private void doCommit() {

		try {
			if (communicater.change(strOld, strNew)) {
				Message msg = handler.obtainMessage(CHANGE_PASSWORD_SUCCESS);
				handler.sendMessage(msg);
			} else {
				Message msg = handler.obtainMessage(CHANGE_PASSWORD_FAILED);
				handler.sendMessage(msg);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Message msg = handler.obtainMessage(Config.CONNECTION_FAIL);
			handler.sendMessage(msg);
		}
	}

	// 清除用户信息
	public void clearUserInfo() {
		UserInfo.getUserInfo().setUserBalance(Config.NULLMARK);
		UserInfo.getUserInfo().setUserFreeze(Config.NULLMARK);
		UserInfo.getUserInfo().setUserSession(Config.NULLMARK);
		UserInfo.getUserInfo().setUserPwd(Config.NULLMARK);
		UserInfo.getUserInfo().setUserName(Config.NULLMARK);
		UserInfo.getUserInfo().setUserFreeze(Config.NULLMARK);
	}

	// 清除数据库
	public void clearDataBase() {
		ContentValues cv = new ContentValues();
		cv.put(Key.KEY_NAME, Config.NULLMARK);
		cv.put(Key.KEY_PASSWORD, Config.NULLMARK);
		cv.put(Key.KEY_SAVE, Config.NUM0);

		db = this.openOrCreateDatabase(Key.KEY_DB_ARABBIT2, MODE_PRIVATE, null);
		db.update(Key.KEY_USERS, cv, "_id=?", new String[] { Config.NUM1 });
		db.close();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		EDebug.o(this, "【修改密码页面被关闭】");

		if (isChangePasswordSuccess) {
			startActivity(new Intent().setClass(ChangePasswordAct.this, LoginAct.class));
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(), ConnectionChangeReceiver.getIntentFilter());
	}

}
